package org.zjt.demo.config;

import org.zjt.demo.common.AppProfileInfo;
import org.zjt.demo.common.SessionCounterListener;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;


@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
	private static final Logger LOGGER= Logger.getLogger(WebMvcConfig.class);
	
	@Value("${spring.profiles.active}")
	private  String springProfile;   //必须使用非静态的
	
	@Value("#{systemProperties['os.name']}")
	private  String osName;

	@Autowired
	private RequestMappingHandlerAdapter handlerAdapter;


	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(AppConfigConstants.WEB_STATIC_RESOURCES_LOCATION);
    }
    
    

//    @Override
//    public void configureViewResolvers(ViewResolverRegistry registry) {
//        registry.enableContentNegotiation(new MappingJackson2JsonView());
//        registry.freeMarker().cache(false);
//    }
//
//    @Bean
//    public FreeMarkerConfigurer freeMarkerConfigurer() {
//        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
//        configurer.setTemplateLoaderPath("/WEB-INF/templates");
//        return configurer;
//    }
    
    @Override
	public void addViewControllers(ViewControllerRegistry registry) {
    	registry.addViewController("/errorPage").setViewName("/errorPage");
    	//registry.addViewController("/login").setViewName("login");
	}



	@Bean
    public String initApplicationContext(){
		LOGGER.info("\t\tcurrnet environment: ꧁༺"+springProfile+"༻ ꧂   \tcurrnet paltform:"+osName );
		LOGGER.info(AppProfileInfo.getProfileString(springProfile));
    	return springProfile;
    }


	@Bean
    public RestTemplate restTemplate(){
    	return  new RestTemplate();
	}



	/**
	 * 增加字符串转日期的功能
	 */
	@Autowired
	public void initEditableValidation() {
		ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer();
		if (initializer.getConversionService() != null) {
			GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
			genericConversionService.addConverter(new StringToDateConverter());
		}
	}



	//增加Listener
	@Bean
	public ServletListenerRegistrationBean servletListenerRegistrationBean() {
		return  new ServletListenerRegistrationBean(new SessionCounterListener());
	}



}

